package cn.lele.reggie.controller;

import cn.lele.reggie.common.R;
import cn.lele.reggie.entity.Category;
import cn.lele.reggie.service.CategoryService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 分类管理
 */
@RestController
@RequestMapping("/category")
@Slf4j
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page> selectAllCategory(Integer page,Integer pageSize){
        Page<Category> pa = new Page<>(page, pageSize);
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByAsc(Category::getSort);
        categoryService.page(pa,wrapper);
        return R.success(pa);
    }

    /**
     * 添加菜品或套餐
     * @param categroy
     * @return
     */
    @PostMapping
    public R<String> insert(@RequestBody Category categroy){
        categoryService.save(categroy);
        return R.success("添加成功");
    }

    @DeleteMapping
    public R<String> delete(Long ids){
        categoryService.removeById(ids);
        return R.success("删除成功");
    }

    @PutMapping
    public R<String> update(@RequestBody Category category){
        categoryService.updateById(category);
        return R.success("修改成功");
    }

    /**
     * 在新增菜品和套餐中，下拉列表中展示
     * @param type
     * @return
     */
    @GetMapping("/list")
    public R<List> list(Integer type){
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(type!=null,Category::getType,type).orderByAsc(Category::getSort);
        List<Category> list=categoryService.list(queryWrapper);
        return R.success(list);
    }


}
